package org.csu.jpetstore_cms.controller;

import lombok.extern.slf4j.Slf4j;
import org.csu.jpetstore_cms.pojo.*;
import org.csu.jpetstore_cms.service.CategoryService;
import org.csu.jpetstore_cms.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.csu.jpetstore_cms.controller.PageResult;

@Slf4j
@RestController

@RequestMapping("/categories")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    //用于获取category
    @GetMapping
    public Result GetAll(){
        List<Category> categoryList = categoryService.getall();
        System.out.println(categoryList);
        return Result.success(categoryList);
    }
    //根据itemid 用于获取指定item
    @GetMapping("/search")
    public Result GetItem(@RequestParam String itemId){
        List<Item> itemList = categoryService.getItem(itemId);
        return Result.success(itemList);
    }
    //用于获取全部item
    @GetMapping("/allitem")
    public Result GetAllItem(){
        List<Item> itemList = categoryService.getAllItem();
        return Result.success(itemList);
    }
    //用于显示所有product
//    @GetMapping("/allproduct")
//    public Result GetAllProduct(){
//        List<Product> productList = categoryService.getAllProduct();
//        return Result.success(productList);
//    }
    //根据categoryid来查询指定product
    @GetMapping("/product")
    public Result GetProductByCategoryId(@RequestParam String categoryId){
        List<Product> productList = categoryService.getProductByCategoryId(categoryId);
        return Result.success(productList);
    }
    //根据productid来查询指定item
    @GetMapping("/item")
    public Result GetProduct(@RequestParam String productId){
        List<Item> itemList = categoryService.getItemByProductId(productId);
        return Result.success(itemList);
    }
    //根据itemid删除item
    @DeleteMapping("/deleteItem")
    public Result DeleteByItemId(@RequestParam String itemid){
        System.out.println(itemid);
        categoryService.DeleteByItemId(itemid);
        return Result.success();
    }
    //根据productid删除product和相对应的item
    @DeleteMapping("/product/{productid}")
    public Result DeleteByProductId(@PathVariable String productid){
        categoryService.DeleteByProductId(productid);
        return Result.success();
    }
    //根据categoryid来删除category和对应的item，product
    @DeleteMapping("/category/{categoryid}")
    public Result DeleteByCategoryID(@PathVariable String categoryid){
        categoryService.DeleteByCategoryId(categoryid);
        return Result.success();
    }
    //新增category
    @PostMapping
    public Result CreatCategory(@RequestBody Category category){
        categoryService.CreatCategory(category);
        return Result.success();
    }
    //新增product
    @PostMapping("/product")
    public Result CreatProduct(@RequestBody Product product){
        categoryService.CreatProduct(product);
        return Result.success();
    }
    //新增item


    @PostMapping("/addItem")
    public Result CreatItem(@RequestBody Item item){
        System.out.println("Received item: " + item);
        categoryService.CreatItem(item);
        return Result.success();
    }
    //修改item
    @PutMapping("/editItem")
    public Result UpdateItem(@RequestBody Item item){
        System.out.println("Received item: " + item);
        categoryService.UpdateItem(item);
        return Result.success();
    }
    //修改product
    @PutMapping("/product")
    public Result UpdateProduct(@RequestBody Product product){
        categoryService.UpdateProduct(product);
        return Result.success();
    }
    //修改category
    @PutMapping("/category")
    public Result UpdateCategory(@RequestBody Category category){
        categoryService.UpdateCategory(category);
        return Result.success();
    }
    @GetMapping("/page")//user
    public Result findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        pageNum = (pageNum - 1) * pageSize;
        List<Item> itemList = categoryService.findByPage(pageNum, pageSize);
        int totalCount = categoryService.getTotalCount();

        PageResult<Item> pageResult = new PageResult<>();
        pageResult.setItemList(itemList);
        pageResult.setTotalCount(totalCount);

        return Result.success(pageResult);

    }
    //测试JWT令牌
//    @PostMapping("/login")
//    public Result Login(@RequestBody Account account){
//        Account e = categoryService.Login(account);
//        if(e!=null){
//            Map<String,Object> claims = new HashMap<>();
//            claims.put("password",e.getPassword());
//            claims.put("username",e.getUsername());
//            String jwt = JwtUtils.generateJwt(claims);
//            return Result.success(jwt);
//        }
//        return Result.error("用户名或密码错误！");
//    }
}
